package net.borderwars.util;

import java.util.HashMap;

/**
 * @author ehubbard
 *         Date: Apr 12, 2004
 *         Time: 8:10:11 PM
 */
public class Format {
    private Format () {
    }

    /**
     * Converts a long to a comma delimited string.  Provided by Tom Milner
     *
     * @param number A long value
     * @return The comma delimited string.
     */
    static public String toCommaString (long number) {
        if (Math.abs (number) < 1000) {
            return (Long.toString (number));
        }
        else {
            String s = toCommaString (number / 1000);
            String r = Long.toString (Math.abs (number) % 1000);
            while (r.length () < 3) {
                r = "0" + r;
            }
            s += "," + r;
            return s;
        }
    } // toCommaString()

    static private String[] sizeAbrevs = {"bytes", "kb", "mb", "gb"};

    /**
     * Converts a size (in bytes) into a shorter easier to read version
     *
     * @param size
     * @return Makes a pretty string
     */
    static public String toCommaSizeString (long size) {
        String s = toCommaString (size);
        int commaCount = countChars (s, ',');
        String abrev = sizeAbrevs[commaCount];
        int firstComma = s.indexOf (',');
        if (firstComma == -1) {
            firstComma = s.length ();
        }
        s = s.substring (0, firstComma);
        s = s + abrev;
        return (s);
    }

    private static int countChars (String s, char c) {
        int count = 0;
        for (int x = 0; x < s.length (); x++) {
            char cc = s.charAt (x);
            if (cc == c) {
                count++;
            }
        }
        return (count);
    }

    /**
     * This is a map of pads that we use for rear & front padding
     */
    static final private HashMap<Integer, StringBuffer> pads = new HashMap<Integer, StringBuffer> ();

    /**
     * rear pads a String to a specific Length
     *
     * @param s   The String to append padding
     * @param len The new total size of the string
     * @return The padded string s of size len
     */
    static public String rearPadString (String s, int len) {
        int toAdd = len - s.length ();
        StringBuffer sb = new StringBuffer (s);
        StringBuffer pad = getPad (toAdd);
        return (sb.append (pad).toString ());
    }

    /**
     * Front pads a String to a specific Length
     *
     * @param s   The String to prepend the padding
     * @param len The new total length of the String
     * @return The padded String S of size len
     */
    static public String frontPadString (String s, int len) {
        int toAdd = len - s.length ();
        StringBuffer sb = new StringBuffer (s);
        StringBuffer pad = getPad (toAdd);
        return (pad.append (sb).toString ());

    }

    private static StringBuffer getPad (int toAdd) {
        if (toAdd < 1) {
            toAdd = 0;
        }
        StringBuffer pad;
        synchronized (pads) {
            pad = pads.get (toAdd);
            if (pad == null) {
                StringBuffer newPad = new StringBuffer (toAdd);
                for (int x = 0; x < toAdd; x++) {
                    newPad.append (" ");
                }
                pads.put (toAdd, newPad);
            }
        }
        return pad;
    }

    /**
     * Search for lookFor within orig and replace them with replaceWith.  With a minimum overhead
     *
     * @param orig
     * @param lookFor
     * @param replaceWith
     * @return blah
     */

    public static String searchAndReplace (String orig, String lookFor, String replaceWith) {
        int idx = orig.indexOf (lookFor);
        if (idx < 0) {  // bail out quick before we do anymore work if we don't need to
            return (orig);
        }
        int prev = 0;
        StringBuffer rtn = new StringBuffer ();
        while (idx >= 0) {
            rtn.append (orig.substring (prev, idx)); // grab the non matching stuff
            rtn.append (replaceWith); // put in the replacement string
            prev = idx + lookFor.length (); // skip past the stuff we are replaceing
            idx = orig.indexOf (lookFor, prev); // where is the next thing to match against?
        }
        rtn.append (orig.substring (prev, orig.length ()));
        return (rtn.toString ());
    }
}
